package com.cnseedlingscloud.draw.prize.controller;


import cn.hutool.json.JSONUtil;
import com.cnseedlingscloud.draw.common.JsonResult;
import com.cnseedlingscloud.draw.prize.entity.TurntableDraw;
import com.cnseedlingscloud.draw.prize.entity.TurntablePhone;
import com.cnseedlingscloud.draw.prize.entity.TurntableRecord;
import com.cnseedlingscloud.draw.prize.service.PrizeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


@Controller
@Slf4j
public class PrizeController {
    @Autowired
    private PrizeService prizeService;

    @RequestMapping("/index")
    public String  jspIndex(){
        return "index";
    }

    @RequestMapping("/queryDraw")
    public String queryDraw(HttpServletRequest request){
        List<TurntableDraw>  list = prizeService.queryAllDraw();
        log.info(JSONUtil.toJsonStr(list));
        request.setAttribute("list", list);
        return "draw";
    }
    @RequestMapping("/toAddDraw")
    public String toAddDraw(){
        return "addDraw";
    }
    @RequestMapping("/save")
    public String save(TurntableDraw draw,HttpServletRequest request){
        prizeService.saveDraw(draw);
        return "redirect:queryDraw";
    }

    @RequestMapping("/toEditDraw")
    public ModelAndView toEditDraw(Long id){

        ModelAndView mav = new ModelAndView("editDraw");
        TurntableDraw draw = prizeService.queryById(id);
        mav.addObject("draw",draw);
        return mav;
    }

    @RequestMapping("/edit")
    public String edit(TurntableDraw draw,HttpServletRequest request){
        prizeService.editDraw(draw);
        return "redirect:queryDraw";
    }


    @RequestMapping("/delDraw")
    public String delDraw(Long id,HttpServletRequest request){
        prizeService.delDraw(id);
        return "redirect:queryDraw";
    }


    @RequestMapping("/queryAll")
    public ModelAndView queryAll(){
        ModelAndView mav = new ModelAndView("luck_draw");
        List<TurntableDraw>   drawList = prizeService.queryAllDraw();
        List<TurntableRecord> recordList = prizeService.queryAllRecord();
        List<TurntablePhone> phoneList = prizeService.queryAllPhone();
        log.info(String.valueOf(phoneList.size()));
        Integer phoneCount = prizeService.countAll();
        mav.addObject("drawList",drawList);
        mav.addObject("recordList",recordList);
        mav.addObject("phoneList",phoneList);
        mav.addObject("phoneCount",phoneCount);
        return mav;
    }
    //抽奖
    @RequestMapping("/getLuckyNum")
    @ResponseBody
    public JsonResult getLuckyNum(Long id,Integer luckyNum){
        log.info("id:"+id+"   luckyNum:"+luckyNum);
        JsonResult result=null;
        Integer remain = prizeService.queryRemain(id);
        //log.info("remain:"+remain);
        if(remain<=0){
            result=new JsonResult(false,0,remain);
        }else{
            List<TurntablePhone> phoneList = prizeService.queryAllPhone();
            List<Long> idList = prizeService.queryByRemain();
            //log.info("idList:"+JSONUtil.toJsonStr(idList));
            //抽奖人数大于剩余人数
            if(luckyNum>remain){
                luckyNum=remain;
            }
            Integer [] luckyArray=new Integer[luckyNum];

            //log.info("luckyNum:"+luckyNum);
            for(int i=0;i<luckyNum;i++){
                Long random = BigDecimal.valueOf(Math.ceil(Math.random() * phoneList.size())).longValue();
                if (idList.contains(random)){
                    luckyArray[i]=random.intValue()-1;
                }else{
                    i--;
                }
            }
            log.info("return :"+JSONUtil.toJsonStr(luckyArray));
            result=new JsonResult(true,200,luckyArray);
        }
        return result;

    }
    //保存中奖
    @RequestMapping("/saveLuckyNum")
    @ResponseBody
    public JsonResult saveLuckyNum(Long id,@RequestParam(value = "luckyArray[]")Long[] luckyArray){
        log.info("id:"+id+"=====luckyArray:"+JSONUtil.toJsonStr(luckyArray));
        prizeService.saveLuckyNum(id,luckyArray);
        JsonResult result=new JsonResult(true,200,"OK");
        return result;
    }
}
